package org.springframework.aop.framework.autoproxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJMethodBeforeAdvice;
import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wtcode
 * @Description
 * @date 2025/4/9 - 11:19
 */
public class A17_2 {



    static class Aspect{

        @Before("execution(* foo())")
        public void before1(){
            System.out.println("before1");
        }

        @Before("execution(* foo())")
        public void before2(){
            System.out.println("before2");
        }

        public void after(){
            System.out.println("after");
        }

        public void afterReturning(){
            System.out.println("afterReturning");
        }


        public void afterThrowing(){
            System.out.println("afterThrowing");
        }

        public Object around(ProceedingJoinPoint pjp){
            return null;
        }

    }

    static class Target{
        public void foo(){
            System.out.println("target foo");
        }
    }

    public static void main(String[] args) {

        SingletonAspectInstanceFactory factory = new SingletonAspectInstanceFactory(new Aspect());
        List<Advisor> advisors = new ArrayList<>();
        for (Method method : Aspect.class.getDeclaredMethods()) {
            if (method.isAnnotationPresent(Before.class)) {
                // 切点
                String expression = method.getAnnotation(Before.class).value();
                AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
                pointcut.setExpression(expression);
                // 通知类
                AspectJMethodBeforeAdvice advice = new AspectJMethodBeforeAdvice(method,pointcut,factory);
                Advisor advisor = new DefaultPointcutAdvisor(pointcut,advice);
                advisors.add(advisor);
            }
        }
        for (Advisor advisor : advisors) {
            System.out.println(advisor);
        }


        /*
         * @Before 前置通知会被转换为下面原始的AspectMethodBeforeAdvice形式，该对象包含了如下信息
         *   a.通知类代码从哪里来
         *   b.切点是什么
         *   c.通知对象如何创建，本例共同用一个Aspect对象
         *   类似的通知还有
         *    1.AspectJAroundAdvice(环绕通知)
         *    2.AspectJAfterReturningAdvice
         *    3.AspectJAfterThrowingAdvice
         *    4.AspectJAfterAdvice
         */

    }



}
